package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: fqy
 * Date: 2021/10/19
 * Time: 18:05
 * Describe:
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    @Override
    public int saveOverflowListGoods(OverflowList overflowList, String overflowListGoodsStr) {
        //  保存俩张表
        //  保存t_overflow_list
        int i = overflowListGoodsDao.insertOverflowList(overflowList);
        //  保存t_overflow_list_goods
        String overflowListGoodsJsonStr = overflowListGoodsStr.replace("[", "").replace("]", "");
        OverflowListGoods overflowListGoods = JSON.parseObject(overflowListGoodsJsonStr, OverflowListGoods.class);

        int j = overflowListGoodsDao.insertOverflowListGoods(overflowList,overflowListGoods);
        if (i == 0 || j == 0){
            // 保存失败
            return 0;
        }
        return 1;
    }
    // 根据时间范围查询报溢单
    @Override
    public Map<String, Object> getOverflowList(String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowList> overflowList = overflowListGoodsDao.selectOverflowList(sTime,eTime);
        map.put("rows", overflowList);
        return map;
    }
    // 根据overflowListId 报溢单商品信息
    @Override
    public Map<String, Object> getOverflowListGoods(Integer overflowListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowListGoods> overflowListGoods =overflowListGoodsDao.selectOverflowListGoods(overflowListId);
        map.put("rows",overflowListGoods);
        return map;
    }
}
